package com.szd.controller.user;

import com.github.pagehelper.PageInfo;
import com.szd.entity.Result;
import com.szd.pojo.Member;
import com.szd.pojo.Product;
import com.szd.service.IMemberService;
import com.szd.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {


    @Autowired
    public IProductService productService;
    //跳转到主页面
    @RequestMapping("/main")
    public String main(HttpSession httpSession, Model model) {
        //Member user = (Member)httpSession.getAttribute("USER_SESSION");
        Product user = (Product) httpSession.getAttribute("USER_SESSION");
        model.addAttribute("name", user.getProductName());
        return "main";
    }
    //跳转到首页
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
    //会员列表页面
    @RequestMapping("/productList")
    public String productList(String productName, Model model ,
                              @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<Product> productList = productService.getAllProducts(productName, pageNum, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(productList);
        model.addAttribute("productList",productList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("productName",productName);
        return "productList";
    }
    //跳转到添加用户页面
    @RequestMapping("/addProduct")
    @ResponseBody
    public Result addProduct(Product product){
        Result<Object> result = new Result<>();
        if (product!=null
                && product.getProductName()!=null
                && !product.getProductName().trim().equals("")
        ){
            System.out.println(product);
            //是否重名
            Product product1 = productService.getProductByProductName(product.getProductName());
            if (product1!=null){
                result.setMsg("产品已经存在！");
                result.setSuccess(false);
                return result;
            }
            //int i = sysUserService.addUser(sysUser);
            int i = productService.addProduct(product);
            if (i>0){
                System.out.println("新增成功！");
                result.setMsg("新增成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("新增失败！");
                result.setMsg("新增失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入产品名！");
            result.setMsg("请输入产品名！");
            result.setSuccess(false);
            return result;
        }

    }
    //删除用户
    @RequestMapping("/delProduct")
    @ResponseBody
    public Result delProduct(@RequestParam("id") Integer id){
        Result<Object> result = new Result<>();
        if(id != null){
            //int i = sysUserService.deleteUser(id);
            //int i = memberService.deleteMember(id);
            int i = productService.deleteProduct(id);
            if (i > 0) {
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }
    //批量删除
    @RequestMapping("/delAllProduct")
    @ResponseBody
    public Result delAllProduct(String ids){
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);

        if (ids!=null){
            String[] strArray = ids.split(",");
            List<String> idsList = Arrays.asList(strArray);

            //int i = sysUserService.deleteAllUser(idsList);
            //int i = memberService.deleteAllMember(idsList);
            int i = productService.deleteAllProduct(idsList);
            if (i>0){
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请选择id！");
            result.setMsg("请选择id！");
            result.setSuccess(false);
            return result;
        }
    }
    //根据id查询用户
    @RequestMapping("/getProductById")
    @ResponseBody
    public Result getProductById(Integer id){
        Result<Object> result = new Result<>();
        System.out.println("收到查询数据");
        if(id != null){
            //SysUser user = sysUserService.getUserById(id);
            //Member member = memberService.getMemberById(id);
            Product product = productService.getProductById(id);
            if (product != null){
                result.setData(product);
                System.out.println("查找成功！");
                result.setMsg("查找成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("没有此商品！");
                result.setMsg("没有此商品！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }
    //编辑用户
    @RequestMapping("/editProduct")
    @ResponseBody
    public Result editProduct(Product product){
        Result<Object> result = new Result<>();
        if (product!=null
                && product.getProductName()!=null
                && !product.getProductName().trim().equals("")
                && product.getId()!=null
        ){
            System.out.println(product);

            int i = productService.editProduct(product);
            if (i>0){
                System.out.println("修改成功！");
                result.setMsg("修改成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("修改失败！");
                result.setMsg("修改失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入产品名或id！");
            result.setMsg("请输入用产品或id！");
            result.setSuccess(false);
            return result;
        }

    }
    //用户详情
    @RequestMapping("/detailProduct")
    @ResponseBody
    public Result detailProduct(Integer id){
        Result<Object> result = new Result<>();
        System.out.println("收到查询数据");
        if(id != null){
            //SysUser sysUser = sysUserService.detailUser(id);
            //Member member = memberService.detailMember(id);
            Product product = productService.detailProduct(id);
            if (product!=null){
                result.setData(product);
                System.out.println("查找成功！");
                result.setMsg("查找成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("没有此用户！");
                result.setMsg("没有此用户！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }
}